<?php
/*
 * @Author: your name
 * @Date: 2020-05-25 10:03:24
 * @LastEditTime: 2020-05-26 16:15:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/controller/userManage/UserGroupController.php
 */ 
namespace app\adminapiv3\controller\userManage;

use app\adminapiv3\controller\Base;
use app\adminapiv3\service\userManage\UserGroupService;
use think\facade\Request;
use think\facade\Validate;

class UserGroupController extends Base
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-25 10:04:29
     */
    function list ()
    {
        $UserGroupService = new UserGroupService();
        $data = $UserGroupService->list();

        $this->jsonOutPut($data);
    }

    /**
     * @name: 添加用户组
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-25 10:04:29
     */
    function add ()
    {

        global $logContent;
        $logContent = '添加用户组';

        $input['name'] = Request::post('name');
        $input['company'] = Request::post('company');
        $input['module'] = Request::post('module');


        $validate = Validate::make([
            'name' => 'require|chsAlphaNum|max:100',
            'company' => 'chsAlphaNum|max:255',
        ]);
        if (!$validate->check($input))
        {
            jsonErrorOut($validate->getError(), $this->CODE_FAIL);
        }



        $UserGroupService = new UserGroupService();
        $UserGroupService->add($input);

        $this->jsonOutPut();
    }

    /**
     * @name 详细信息
     * @param $userGroupId
     * @author wx
     * @date 2020/7/22 17:29
     */
    function info()
    {
        $userGroupId = $this->filterInput('user_group_id*', 'post');
        $UserGroupService = new UserGroupService();
        $info = $UserGroupService->info($userGroupId);

        $this->jsonOutPut($info);
    }

    /**
     * @name: 修改用户组
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-25 10:04:29
     */
    function modify ()
    {
        global $logContent;
        $logContent = '修改用户组';

        $input['id']= Request::post('id');
        $input['name'] = Request::post('name');
        $input['company'] = Request::post('company');
        $input['module'] = Request::post('module');

        
        $validate = Validate::make([
            'id' => 'require|number',
            'name' => 'require|chsAlphaNum|max:100',
            'company' => 'chsAlphaNum|max:255',
        ]);
        if (!$validate->check($input))
        {
            return json_encode([
                'code' => $this->CODE_FAIL,
                'msg' => $validate->getError()
            ]);
        }
        $UserGroupService = new UserGroupService();
        $UserGroupService->modify($input['id'], $input);

        $this->jsonOutPut();
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-25 10:05:28
     */
    function delete ()
    {
        global $logContent;
        $logContent = '删除用户组';

        $input['user_group_id']= Request::post('user_group_id');


        $validate = Validate::make([
            'user_group_id' => 'require|number',
        ]);
        if (!$validate->check($input))
        {
            return json_encode([
                'code' => $this->CODE_FAIL,
                'msg' => $validate->getError()
            ]);
        }
        
        $UserGroupService = new UserGroupService();
        $UserGroupService->delete($input['user_group_id']);

        $this->jsonOutPut();
    }

    /**
     * @name 登录限制
     * @author wx
     * @date 2020/6/4 16:30
     */
    public function loginSetting()
    {
        global $logContent;
        $logContent = '登录限制';

        $data['fail_count'] = $this->filterInput('fail_count*','post',5);
        $data['fail_time'] = $this->filterInput('fail_time*','post',86400);

        $fileName = './config/login.conf';

        file_put_contents($fileName, json_encode($data));

        $this->jsonOutPut();
    }

    /**
     * @name 获取登录限制
     * @author wx
     * @date 2020/6/4 16:30
     */
    public function getLoginSetting()
    {
        $fileName = './config/login.conf';

        if(!file_exists($fileName)){
            $data['fail_count'] = 5;
            $data['fail_time'] = 86400;
            file_put_contents($fileName, json_encode($data));
        }

        $info = json_decode(file_get_contents($fileName),true);

        $this->jsonOutPut($info);
    }

    /**
     * @name 返回授权模块
     * @author wx
     * @date 2020/11/12 10:29
     */
    public function module()
    {

        $UserGroupService = new UserGroupService();
        $data = $UserGroupService->module();

        $this->jsonOutPut($data);
    }


}